package com.xhsj.user.hr.process.hrEntryProcess.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.hr.aspect.OperationPointCut;
import com.xhsj.user.hr.process.dto.hrEntryProcess.MyEntryApplyListDto;
import com.xhsj.user.hr.process.dto.hrEntryProcess.ReturnEntryApplyDto;
import com.xhsj.user.hr.process.dto.hrEntryProcess.RunEntryApplyListDto;
import com.xhsj.user.hr.process.dto.hrEntryProcess.StartEntryApplyDto;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.process.hrEntryProcess.entity.TbHrApprovalEntryApply;
import com.xhsj.user.hr.process.hrEntryProcess.query.CompletedEntryApplyQuery;
import com.xhsj.user.hr.process.hrEntryProcess.query.MyEntryApplyDetailedListQuery;
import com.xhsj.user.hr.process.hrEntryProcess.query.MyEntryApplyListQuery;
import com.xhsj.user.hr.process.hrEntryProcess.query.RunEntryApplyListQuery;
import com.xhsj.user.hr.process.hrEntryProcess.service.TbHrApprovalEntryApplyService;
import com.xhsj.user.hr.process.message.service.TbProjectMessageService;
import com.xhsj.user.hr.process.query.ApprovalQuery;
import com.xhsj.user.utils.Message;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import java.util.concurrent.CompletableFuture;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.validation.Valid;


/**
 * <p>
 * hr流程-入职申请表 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Sep 7, 2020 10:42:13 AM
 */
@Slf4j
@Api(tags = "hr流程-入职申请表")
@RestController
@RequestMapping("/hrEntryProcess/tbHrApprovalEntryApply")
public class TbHrApprovalEntryApplyController {

	@Autowired
    public TbHrApprovalEntryApplyService service;

	// 项目消息 服务实现类
	@Autowired
	public TbProjectMessageService tbProjectMessageService;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Sep 7, 2020 10:42:13 AM
	 */
	@ApiOperation(value = "hr流程-入职申请表,保存/修改", notes = "hr流程-入职申请表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:edit')")
	public Message save(@RequestBody TbHrApprovalEntryApply data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Sep 7, 2020 10:42:13 AM
	 */
	@ApiOperation(value = "hr流程-入职申请表删除", notes = "通过id删除hr流程-入职申请表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Sep 7, 2020 10:42:13 AM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Sep 7, 2020 10:42:13 AM
    */
	@ApiOperation(value = "分页查询hr流程-入职申请表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbHrApprovalEntryApply> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Sep 7, 2020 10:42:13 AM
    */
	@ApiOperation(value = "查询hr流程-入职申请表所有数据list", notes = "查询hr流程-入职申请表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Sep 7, 2020 10:42:13 AM
    */
	@ApiOperation(value = "通过id获取hr流程-入职申请表实体类", notes = "通过id获取hr流程-入职申请表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Sep 7, 2020 10:42:13 AM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}


	/**
	 * 我的入职申请删除
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的入职申请删除", notes = "我的入职申请删除")
	@GetMapping(value="/deleteMyEntryApply")
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:delete')")
	public Message deleteMyEntryApply(@RequestParam(value = "dataId") Long dataId,
									  @RequestParam(value = "interviewRegistrationId") Long interviewRegistrationId) throws Exception {
		log.info("我的入职申请删除参数{}", dataId);
		service.deleteMyEntryApply(dataId,interviewRegistrationId);
		return Message.success("成功");
	}

	/**
	 * 我的入职申请修改
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的入职申请修改", notes = "我的入职申请修改")
	@PostMapping(value="/updateMyEntryApply")
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:edit')")
	public Message updateMyEntryApply(@RequestBody StartEntryApplyDto data) throws Exception {
		log.info("我的入职申请修改参数{}", data);
		try {
			service.updateMyEntryApply(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("失败:{}",e);
			return Message.error(1,e.getMessage());
		}

	}

	/**
	 * hr流程-入职申请开始
	 * @author liuqinghua
	 * @date Sep 7, 2020 10:42:13 AM
	 */
	@OperationPointCut(module = "HR模块",operation = "入职申请流程启动")
	@ApiOperation(value = "hr流程-入职申请开始", notes = "hr流程-入职申请开始")
	@RequestMapping(value = "/startEntryApply", method = RequestMethod.POST)
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:edit')")
	public Message startEntryApply(@RequestBody StartEntryApplyDto data){
		try {
			ApprovalQuery approvalQuery = service.startEntryApply(data);

			//保存消息
			if (data.getApply().getSubmitStauts() == 1) {
				CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
					tbProjectMessageService.sendMessageCommon(approvalQuery);
				});
			}

			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}
	}

	/**
	 * hr流程-入职申请重新提交
	 * @author liuqinghua
	 * @date Sep 7, 2020 10:42:13 AM
	 */
	@ApiOperation(value = "hr流程-入职申请重新提交", notes = "hr流程-入职申请重新提交")
	@RequestMapping(value = "/returnEntryApply", method = RequestMethod.POST)
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:edit')")
	public Message returnEntryApply(@RequestBody ReturnEntryApplyDto data){
		try {
			ApprovalQuery approvalQuery = service.returnEntryApply(data);
			CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
				tbProjectMessageService.sendMessageCommon(approvalQuery);
			});
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}
	}

	/**
	 * 入职流程审批
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@OperationPointCut(module = "HR模块",operation = "入职流程审批")
	@ApiOperation(value = "入职流程审批", notes = "入职流程审批")
	@PostMapping(value="/approvalEntryApply")
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:aduit')")
	public Message approvalEntryApply(@RequestBody @Valid HandleAuditDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery =  service.approvalEntryApply(data);
		CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
			tbProjectMessageService.sendMessageCommon(approvalQuery);
		});
		return Message.success("成功");
	}

	/**
	 * 入职流程作废
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@OperationPointCut(module = "HR模块",operation = "入职流程作废")
	@ApiOperation(value = "入职流程作废", notes = "入职流程作废")
	@PostMapping(value="/approvalEntryToVoidApply")
	public Message approvalEntryToVoidApply(@RequestBody @Valid HandleAuditDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery = service.approvalEntryToVoidApply(data);
		return Message.success("成功");
	}

	/**
	 * 我的入职申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的入职申请列表", notes = "我的入职申请列表")
	@PostMapping(value="/myEntryApplyList")
	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:list')")
	@OperationPointCut(module = "HR模块",operation = "入职列表查看")
	public Message myEntryApplyList(@RequestBody @Valid MyEntryApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("我的入职申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		Page<MyEntryApplyListQuery> myLeaveApplyListQueryPage = service.myEntryApplyList(data);
		return Message.success("成功",myLeaveApplyListQueryPage.getRecords(),myLeaveApplyListQueryPage.getTotal(),myLeaveApplyListQueryPage.getPages());
	}

	/**
	 * 待审核入职申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "待审核入职申请列表", notes = "待审核入职申请列表")
	@PostMapping(value="/runEntryApplyList")
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:run')")
	public Message runEntryApplyList(@RequestBody @Valid RunEntryApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("待审核入职申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		Page<RunEntryApplyListQuery> myLeaveApplyListQueryPage = service.runEntryApplyList(data);
		return Message.success("成功",myLeaveApplyListQueryPage.getRecords(),myLeaveApplyListQueryPage.getTotal(),myLeaveApplyListQueryPage.getPages());
	}

	/**
	 * 已审核入职申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "已审核入职申请列表", notes = "已审核入职申请列表")
	@PostMapping(value="/completedEntryApplyList")
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:completed')")
	public Message completedEntryApplyList(@RequestBody @Valid RunEntryApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("已审核入职申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<CompletedEntryApplyQuery> completedLeaveApplyList = service.completedEntryApplyList(data);
		return Message.success("成功",completedLeaveApplyList.getRecords(),completedLeaveApplyList.getTotal(),completedLeaveApplyList.getPages());
	}

	/**
	 * 我的入职申请详细信息
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的入职申请详细信息", notes = "我的入职申请详细信息")
	@GetMapping(value="/myEntryApplyDetailedList")
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:detailed:list')")
	public Message myEntryApplyDetailedList(@RequestParam(value = "dataId") Long dataId) throws Exception {
		log.info("我的请假申请详细信息参数{}", dataId);
		MyEntryApplyDetailedListQuery myLeaveApplyListQuery = service.myEntryApplyDetailedList(dataId);
		return Message.success("成功",myLeaveApplyListQuery);
	}

	/**
	 * 我的入职申请详细信息通过面试登记id
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的入职申请详细信息通过面试登记id", notes = "我的入职申请详细信息通过面试登记id")
	@GetMapping(value="/myEntryApplyDetailedListByRegistrationId")
	public Message myEntryApplyDetailedListByRegistrationId(@RequestParam(value = "interviewRegistrationId") Long interviewRegistrationId,
															@RequestParam(value = "entryId",required = false) Long entryId) throws Exception {
		log.info("我的入职申请详细信息通过面试登记id参数{}", interviewRegistrationId);
		Object myLeaveApplyListQuery = service.myEntryApplyDetailedListByRegistrationId(interviewRegistrationId,entryId);
		return Message.success("成功",myLeaveApplyListQuery);
	}


	/**
	 * 查询入职时间
	 * @author 苏小林
	 * @date 2020/9/15 10:38
	 * @param: userId
	 */
	@ApiOperation(value = "查询入职时间", notes = "查询入职时间")
	@GetMapping(value="/findArrivalDate")
	public Message findArrivalDate(@RequestParam("userId") Long userId) {

		try {
			String arrivalDate = service.findArrivalDate(userId);
			return Message.success("成功",arrivalDate);
		} catch (Exception e) {
			log.error("失败:{}",e);
			return Message.error(1,"失败");
		}
	}

}

